/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nexus.adec;
import control.*;
import java.util.*;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.el.VariableResolver;
import javax.faces.event.ActionEvent;
import javax.swing.JOptionPane;
import org.apache.myfaces.custom.datascroller.ScrollerActionEvent;
import nexus.adec.recursos.SeleccionGrupo;
/**
 *
 * @author juampi
 */
public class EditarUsuario {
    private Usuario usuario;
    private ArrayList<SeleccionGrupo> seleccionarGrupo;
    private Long rowCount = new Long(10);
    private String filtro;
    public void setActual(Usuario u){
        usuario=u;
    }
    public void setFiltro(String f){
        filtro=f;
    }
    public String getFiltro(){
        return filtro;
    }
    public ArrayList<Usuario> getUsuarios(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        VariableResolver vr = facesContext.getApplication().getVariableResolver();
        UserBean u = (UserBean)vr.resolveVariable(facesContext, "userBean");
        
        if(filtro==null||filtro.equals("")){
            return GestorEspacioDB.getInstance().buscarUsuarios(u.getCodigoCurso(), u.getId(),"");
        }else{
            return GestorEspacioDB.getInstance().buscarUsuarios(u.getCodigoCurso(), u.getId(),filtro);
        }
    }
    public ArrayList<SeleccionGrupo> getGrupos(){
        /*FacesContext facesContext = FacesContext.getCurrentInstance();
        VariableResolver vr = facesContext.getApplication().getVariableResolver();
        UserBean u = (UserBean)vr.resolveVariable(facesContext, "userBean");
        seleccionarGrupo=new  ArrayList<SeleccionGrupo>();
        ArrayList<String> grupoUsuario=GestorEspacioDB.getInstance().listarGrupos(usuario.getNombreUsuario());
        ArrayList<String> grupoEspacio=GestorEspacioDB.getInstance().listarGrupos(u.getCodigoCurso());
        for(String s: grupoEspacio){
            seleccionarGrupo.add(new SeleccionGrupo(s,grupoUsuario.contains(s)));
        }
        return seleccionarGrupo;*/
        return null;
    }
    
    public void editar(ActionEvent ae){
        Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        UIParameter component = (UIParameter) ae.getComponent().findComponent("codigoEditar");
        String idUsuario = component.getValue().toString();
        setActual(GestorEspacioDB.getInstance().obtenerUsuario(idUsuario));
    }
    public void eliminar(ActionEvent ae){
        Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        UIParameter component = (UIParameter) ae.getComponent().findComponent("codigoEliminar");
        String idUsuario = component.getValue().toString();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        VariableResolver vr = facesContext.getApplication().getVariableResolver();
        UserBean u = (UserBean)vr.resolveVariable(facesContext, "userBean");
        GestorEspacioDB.getInstance().quitarUsuario(u.getCodigoCurso(),idUsuario);
    }
    public void guardarCambios(ActionEvent ae){
    }
    public Long getRowCount() {
        return rowCount;
    }

    public void setRowCount(Long rowCount) {
        this.rowCount = rowCount;
    }
    public String update(){
        setFiltro(filtro);
        return "ok";
    }
    public void scrollerAction(ActionEvent event)
    {
        ScrollerActionEvent scrollerEvent = (ScrollerActionEvent) event;
        FacesContext.getCurrentInstance().getExternalContext().log(
                        "scrollerAction: facet: "
                                        + scrollerEvent.getScrollerfacet()
                                        + ", pageindex: "
                                        + scrollerEvent.getPageIndex());
    }

}
